package com.ww.yaodao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ww.yaodao.mapper.ImportProductMapper;
import com.ww.yaodao.mapper.ProductMapper;
import com.ww.yaodao.mapper.TypeMapper;
import com.ww.yaodao.model.bo.ImportProductDO;
import com.ww.yaodao.model.bo.ProductDO;
import com.ww.yaodao.model.bo.TypesDO;
import com.ww.yaodao.model.vo.imports.ImportRespVO;
import com.ww.yaodao.model.vo.imports.ImportVO;
import com.ww.yaodao.service.ImportProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ImportProductServiceImpl implements ImportProductService {
    @Autowired
    private ImportProductMapper importProductMapper;

    @Override
    public ImportRespVO queryImportProduct(ImportVO importVO) {
        LambdaQueryWrapper<ImportProductDO> wrapper = new LambdaQueryWrapper<>();
        if (importVO!=null){
            if (!StringUtils.isEmpty(importVO.getIusername())){
                wrapper.like(ImportProductDO::getIusername,importVO.getIusername());
            }
            if (!StringUtils.isEmpty(importVO.getTranceno())){
                wrapper.eq(ImportProductDO::getTranceno,importVO.getTranceno());
            }
        }
        List<ImportProductDO> buyDOS = importProductMapper.selectList(wrapper);
        ImportRespVO build = ImportRespVO.builder().list(buyDOS).build();
        return build;
    }

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private TypeMapper typeMapper;

    @Override
    public ImportRespVO insertProduct(ImportVO importVO) {
        if (importVO.getPcount() != null
                && importVO.getPdes() != null && importVO.getPname() != null
                && importVO.getPprice() != null && importVO.getPtype() != null
                && importVO.getIusername() != null && importVO.getItime() != null
                && importVO.getMoney() != null
                && importVO.getEmployee() != null && importVO.getTranceno() != null
        ) {
            int insert = 0;
            int row = 0;

            //添加药品
            ProductDO productDO = new ProductDO();
            productDO.setPcount(importVO.getPcount());
            productDO.setPdes(importVO.getPdes());
            productDO.setPname(importVO.getPname());
            productDO.setPprice(importVO.getPprice());
            Integer tid = getTid(importVO.getPtype());

            //添加订单
            ImportProductDO build = ImportProductDO.builder().eaddress(importVO.getEaddress())
                    .iusername(importVO.getIusername()).itime(importVO.getItime())
                    .money(importVO.getMoney()).employee(importVO.getEmployee())
                    .tranceno(importVO.getTranceno()).build();

            if (tid == 0) {
                TypesDO build1 = TypesDO.builder().type(importVO.getPtype())
                        .tname(importVO.getIusername()).tdate(importVO.getItime()).build();
                insert = typeMapper.insert(build1);
                tid = build1.getTid();
            }
            productDO.setPtype(tid);
            //插入库存
            insert = productMapper.insert(productDO);

            int insert1 = importProductMapper.insert(build);

            if (insert == 1 && insert1 == 1) {
                row = 1;
            }
            ImportRespVO build1 = ImportRespVO.builder().row(row).list(queryImportProduct(null).getList()).code("2000").build();
            return build1;
        } else {
            return ImportRespVO.builder().code("1000").build();
        }
    }

    @Override
    public ImportRespVO updateImport(ImportVO importVO) {
        Integer id = importVO.getId();
        LambdaQueryWrapper<ImportProductDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ImportProductDO::getId, importVO.getId());
        ImportProductDO importProductDO = new ImportProductDO();
        BeanUtils.copyProperties(importVO, importProductDO);
        int update = importProductMapper.update(importProductDO, wrapper);
        ImportRespVO importRespVO = queryImportProduct(null);
        importRespVO.setCode("2000");
        importRespVO.setRow(update);
        return importRespVO;
    }

    public Integer getTid(String type) {
        LambdaQueryWrapper<TypesDO> typesDOQueryWrapper = new LambdaQueryWrapper<>();
        TypesDO typesDO = null;
        if (type != "") {
            typesDOQueryWrapper.like(TypesDO::getType, type);
            typesDO = typeMapper.selectOne(typesDOQueryWrapper);
            if (typesDO != null) {
                return typesDO.getTid();
            }
        }
        return 0;
    }
}
